﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using iMapa.UI.ModelView.Main;
using iMapa.UI.ModelView.Main.Wizard;
using iMapa.Backend;
using iMapa.Utils;

namespace iMapa.UI.View.Main.Wizard
{
    /// <summary>
    /// Interaction logic for Ws3CategorySelect.xaml
    /// </summary>
    public partial class Ws3CategorySelect : UserControl,IWizardStep
    {
        public Ws3CategorySelect()
        {
            InitializeComponent();
        }

        #region IWizardStep Members

        public bool IsNextStepValid()
        {
           return true;
        }

        public void SetError()
        {
            return;
        }

        public void Finish()
        {
            NewIMapWizardViewModel model = (NewIMapWizardViewModel)this.DataContext;
            InteractMap map = new InteractMap();
            Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte> image = new Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte>(ImgUtils.GetBitmap(model.Image));
            image = image.Resize(CountScale(image),Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
            map.ModelImage = image;
            map.Name = model.MapName;
            map.ParentGroup = model.ParentContext.Groups[model.SelectedGroupIndex] == null ? "Enter Name..." : model.ParentContext.Groups[model.SelectedGroupIndex].Name;
            map.InitializeInteractMap();
            model.ParentContext.AddNewMap(map);
            return;
        }

        private double CountScale(Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte> image)
        {
            int h =image.Height;
            int w = image.Width;
            int maxh = ConfigurationConstants.MAXIMAL_IMAP_MODEL_HEIGHT;
            int maxw = ConfigurationConstants.MAXIMAL_IMAP_MODEL_WIDTH;
            double scaleH = (double)maxh / (double)h;
            double scaleW = (double)maxw / (double)w;
            return scaleH > scaleW ? scaleW : scaleH;
        }

        #endregion
    }
}
